<?php
class CommentairesController extends AppController {
	public $components = array('RequestHandler');

	public function add() {
        $this->layout = null;
        if ($this->request->is('post')) {
        	if(AppController::isConnected($this->request->data['username'], $this->request->data['token'])){
	        	$data = array('Commentaire' => array('user_id' => $this->request->data['user_id'], 'product_id' => $this->request->data['product_id'], 'commentaire' => $this->request->data['commentaire'], 'note' => $this->request->data['note']));
	        	if($this->Commentaire->save($data)){
	                $result = new \stdClass();
	                $result->code = 0;
	                $result->body = "Commentaire bien enregistré";
	                $this->set(array(
	                    'result' => $result,
	                    '_serialize' => array('result')
	                ));
	            }else{
	                $result = new \stdClass();
	                $result->code = 1;
	                $result->body = "Erreur lors de l'enregistrement du commentaire";
	                $this->set(array(
	                    'result' => $result,
	                    '_serialize' => array('result')
	                ));
	            }
        	}else{
        		$result = new \stdClass();
	            $result->code = 2;
	            $result->body = "Utilisateur déconnecté";
	            $this->set(array(
	                'result' => $result,
	                '_serialize' => array('result')
	            ));
        	}
        }
	}
}
